from .das.stock_day_das import StockDateDayDas
import inspect
from dto.stock_risk_assessment import RiskAssessmentDTO
from .strategy.base_strategy import BaseStrategy
from .das.stock_assessment_das import RiskAssessmentService
from .das.stock_day_das import StockDateDayDas


class StrategyService:
    def __init__(self):
        self.risk_service = RiskAssessmentService()
        self.stock_data_service = StockDateDayDas()

    def run_all_strategies(self, stock_code):

        # 查找 BaseStrategy 的所有子类
        strategies = BaseStrategy.__subclasses__()

        for strategy_cls in strategies:

            # 初始化策略并调用分析方法
            strategy = strategy_cls()

            # 拿到需要拉取的股票数据
            stock_day_num = strategy.checkDay()
            stock_data_info = self.stock_data_service.search_by_day(stock_code,stock_day_num)

            result = strategy.analyze(stock_data_info)

            # 将分析结果保存到数据库
            self.save_risk_assessment(result)

    def save_risk_assessment(self, result):
        # 假设每个策略返回一个包含风险描述的结果
        dto = RiskAssessmentDTO(
            stock_code=result['stock_code'],
            description=result['description'],
            risk_type="策略分析",
            risk_level=5,  # 假设是中等风险
            stock_hold_ratio=50,  # 假设持股建议比例为 50%
            future_action="持有",  # 示例操作
            stock_tag_code="STRATEGY_ANALYSIS",
            stock_tag_desc="策略分析结果"
        )
        self.risk_service.add_risk_assessment(dto)
